package com.yunhe.abnormal.service.base.impl;

import com.yunhe.abnormal.client.MeasRealtimeClient;
import com.yunhe.abnormal.domain.base.AlarmLevel;
import com.yunhe.abnormal.mapper.base.AlarmLevelMapper;
import com.yunhe.common.model.base.AlarmLevelDTO;
import com.yunhe.abnormal.repository.base.AlarmLevelRepository;
import com.yunhe.abnormal.service.base.AlarmLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class AlarmLevelServiceImpl implements AlarmLevelService {

    @Autowired
    private AlarmLevelRepository alarmLevelRepository;
    @Autowired
    private AlarmLevelMapper alarmLevelMapper;
    @Autowired
    private MeasRealtimeClient measRealtimeClient;

    @Override
    public AlarmLevelDTO create(AlarmLevel alarmLevel) {
        alarmLevel.setId(null);
        AlarmLevel result = alarmLevelRepository.save(alarmLevel);
        return alarmLevelMapper.to(result);
    }

    @Override
    public AlarmLevelDTO findByCode(Long code) {
        AlarmLevel result = alarmLevelRepository.findByCode(code);
        return alarmLevelMapper.to(result);
    }

    @Override
    public AlarmLevelDTO findById(Long id) {
        AlarmLevel result = alarmLevelRepository.findById(id).orElse(null);
        return alarmLevelMapper.to(result);
    }

    @Override
    public List<AlarmLevelDTO> findAll() {
        List<AlarmLevel> result = (List<AlarmLevel>) alarmLevelRepository.findAll();
        return alarmLevelMapper.to(result);
    }

    @Override
    public AlarmLevelDTO findAlarmLevelByAlarmId(Long abnormalId) {
        AlarmLevel result = alarmLevelRepository.findAlarmLevelByAlarmId(abnormalId);
        return alarmLevelMapper.to(result);
    }

    @Override
    public AlarmLevelDTO findAlarmLevelByAlarmName(String alarmName) {
        AlarmLevel result = alarmLevelRepository.findAlarmLevelByAlarmName(alarmName);
        return alarmLevelMapper.to(result);
    }

    @Override
    public void delete(Long id) {
        alarmLevelRepository.deleteById(id);
    }

    @Override
    public List<String> getAlarmLevelNameByPointNumbers(List<Long> pointNumbers, Long stationId) {
        List<String> result = alarmLevelRepository.findAlarmLevelNamesByPointNumbers(pointNumbers, stationId);
        return result;
    }

    @Override
    public List<String> getAlarmLevelNameByObjectId(Long objectId) {
        List<Long> pointNumbers = alarmLevelRepository.findAlarmPointNumbersByObjectId(objectId);
        Map<Long, Double> pnValueMap = measRealtimeClient.getMeasureRealtime(pointNumbers).getBody().getResults();
        if (pnValueMap == null) {
            return null;
        }
        Set<Long> entries = pnValueMap.keySet();
        List<Long> pointNumbersAfterProcess = new ArrayList<>();
        for (Long entry : entries) {
            Double value = pnValueMap.get(entry);
            if (value != null && value.equals(1.0)) {
                pointNumbersAfterProcess.add(entry);
            }
        }
        if (pointNumbersAfterProcess != null && pointNumbersAfterProcess.size() != 0) {
            List<String> result = alarmLevelRepository.findAlarmLevelNamesByPointNumbers(pointNumbersAfterProcess);
            return result;
        } else {
            return null;
        }
    }

    @Override
    public AlarmLevel findByName(String name) {
        return alarmLevelRepository.findByName(name);
    }
}
